/*
 * Filename:    HcpmsOperateFile.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年12月6日
 */
package com.paic.mhis.hcpms.itextpdf.web.contoller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.paic.mhis.hcpms.itextpdf.web.pdfmodel.DocumentModel;

public class HcpmsOperateFile {
	private Log log = LogFactory.getLog(this.getClass());
	private String fileName;
	private String fullPath;

	public HcpmsOperateFile(String filePath, String fileName) {
		StringBuffer path = new StringBuffer();
		path.append(filePath).append("temp").append(File.separator);
		File foder = new File(path.toString());
		if (!foder.exists()) {
			foder.mkdir();
		}
		path.append(fileName);

		File file = new File(path.toString());
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		this.fileName = fileName;
		this.fullPath = path.toString();
	}

	public void DownFile(HttpServletResponse response) throws Exception {
		BufferedOutputStream bos = null;
		String disposition = "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8");
		response.setHeader("Content-disposition", disposition);
		FileInputStream fis = null;
		File file = null;
		try {
			file = new File(fullPath);
			fis = new FileInputStream(file);
			bos = new BufferedOutputStream(response.getOutputStream());
			byte[] buffer = new byte[2048];
			while (fis.read(buffer) != -1) {
				bos.write(buffer);
				bos.flush();
			}
		} catch (FileNotFoundException e) {
			log.info(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			log.info(e.getMessage());
			e.printStackTrace();
		} finally {
			if (fis != null) {
				fis.close();
			}
			if (bos != null) {
				bos.close();
			}
			// if (file != null) {
			// file.delete();
			// }
		}
	}

	public void careatePdfFile(DocumentModel model, Map<String, Object> map) {
		// FileOutputStream fos = null;
		Document document = new Document();
		try {
			// fos = ;
			model.createDocument(document);
			PdfWriter pdf = PdfWriter.getInstance(document, new FileOutputStream(fullPath));
			document.open();
			model.setPdfWriter(pdf, map);
			model.createContent(document, map);
			// fos.flush();
			document.close();
		} catch (DocumentException e) {
			log.info(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			log.info(e.getMessage());
			e.printStackTrace();
		}
		// finally {
		// if (fos != null) {
		// try {
		// fos.flush();
		// fos.close();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		// }
		// }
	}

	public void DownPdfFile(DocumentModel model, Map<String, Object> map, HttpServletResponse response)
			throws Exception {
		careatePdfFile(model, map);
		DownFile(response);
	}

	public void DownPdfFile1(DocumentModel model, Map<String, Object> map, HttpServletResponse response)
			throws Exception {
		BufferedOutputStream bos = null;
		String disposition = "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8");
		response.setHeader("Content-disposition", disposition);
		FileInputStream fis = null;
		File file = null;
		try {

			file = new File(fullPath);
			fis = new FileInputStream(file);
			bos = new BufferedOutputStream(response.getOutputStream());
			byte[] buffer = new byte[2048];
			while (fis.read(buffer) != -1) {
				bos.write(buffer);
				bos.flush();
			}
		} catch (FileNotFoundException e) {
			log.info(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			log.info(e.getMessage());
			e.printStackTrace();
		} finally {
			if (fis != null) {
				fis.close();
			}
			if (bos != null) {
				bos.close();
			}
			// if (file != null) {
			// file.delete();
			// }
		}
	}

	public void openPdfFile(HttpServletResponse response) throws Exception {
		BufferedInputStream inputStream = null;
		OutputStream out = null;
		String disposition = "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8");
		response.setHeader("Content-disposition", disposition);
		// FileInputStream fis = null;
		File file = null;
		try {

			file = new File(fullPath);
			inputStream = new BufferedInputStream(new FileInputStream(file));
			out = response.getOutputStream();
			byte[] b = new byte[1024];
			int len = 0;
			response.reset();
			out = response.getOutputStream();
			while ((len = inputStream.read(b)) > 0) {
				out.write(b, 0, len);
			}
			out.flush();
			inputStream.close();
			out.close();
		} catch (FileNotFoundException e) {
			log.info(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			log.info(e.getMessage());
			e.printStackTrace();
		} finally {
			if (inputStream != null) {
				inputStream.close();
			}
			if (out != null) {
				out.close();
			}
		}
	}
}
